import {Component, Input} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {ActivatedRoute} from "@angular/router";
declare let MathJax;
@Component({
    selector:"subjects-card",
    template:`
      <div class="card" [style.backgroundColor]="flag==1?'darkgreen':'darkorange'">
        <div class="card-header" >
          <img src="./assets/logo-trans.png">
          <div>
            <h3>{{knowledge.knowledge}}</h3> 
            <small>{{ndate.getFullYear()}}-{{ndate.getMonth()+1}}-{{ndate.getDate()}}</small>
          </div>
        </div>
        <div class="card-content">
          <ol>
            <li *ngFor="let s of ss">{{s.content}}</li>
          </ol>
        </div>
        <div class="card-footer">
          <div>
            <p>岁月惋惜，譬诸逝水。&nbsp;&nbsp;&nbsp;&nbsp;</p> 
            <p>&nbsp;&nbsp;&nbsp;&nbsp;当饱览机要，以济功业</p> 
          </div>
          <img width="100" height="100" src="./assets/qr.jpg">
        </div>
      </div>
    `,
    styleUrls:['subjects.card.component.css']
  })
export class SubjectsCardComponent{

  ndate=new Date();
  // subjects={
  //   type:"knowledge",
  //   title:"有理数指数幂",
  //   details:[
  //     {content:"",level:4}
  //   ]
  // };
  ss=[
    {content:"解不等式：$2^{x+\\\dfrac{1}{x}+1}\\\le \\\dfrac{1}{2}$"},
    {content:"若$3^a=0.618,a\\\in[k,k+1),(k\\\in Z)$,求k"},
    {content:"已知$0\\\lt a \\\lt 1$,$0\\\lt b \\\lt 1$，解不等式:$a^{log_b(x-3)}<1$"}
  ];

  @Input() knowledge:any={id:1,title:"指数函数单调性应用"}
  @Input() flag=1;
  constructor(private http:HttpClient,private router:ActivatedRoute){
    this.router.params.subscribe(pars=>{
      if(pars["flag"]) this.flag=pars["flag"];
      if(pars["knowledge"]){
        this.http.get(`http://www.defangit.com:30002/api/knowledge/${pars["knowledge"]}`)
          .toPromise().then(k=>{
            this.knowledge=k[0];
            this.getData();
        })
      }


    })

  }
  getData(){
    this.http.get<any[]>(`http://www.defangit.com:30002/api/details?_where=(knowledgeId,eq,${this.knowledge.id})~and(flag,eq,${this.flag})`)
      .toPromise()
      .then(res=>{
        this.ss=res;
        setTimeout(()=>MathJax.Hub.Queue(["Typeset",MathJax.Hub]),300)
      });
    // this.http.post<any[]>("http://www.defangit.com:30002/dynamic",{
    //     "query": "select * from details where flag=?? and knowlegeid=?? limit 1,20",
    //     "params": [1,1]
    //   }).toPromise()
    //   .then(res=>{
    //     console.dir(res);
    //   })
  }
}
